package com.steam.job;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.github.pagehelper.PageHelper;
import com.google.gson.Gson;
import com.steam.entity.Phone;
import com.steam.entity.SmssSendmsg;
import com.steam.entity.SmssSendmsgExample;
import com.steam.mapper.ItemMapper;
import com.steam.mapper.SmssSendmsgMapper;
import com.steam.service.JedisClient;
import com.steam.util.GsonBuilderUtil;
//@Component
public class WeihaiSpringJob {
	@Autowired
	private RabbitTemplate rabbitTemplate;
	@Autowired
	private JedisClient jedisClient;
	@Autowired
	private ItemMapper itemMapper;
	
	private static SqlSessionFactory sqlSessionFactory;
	Gson gson = GsonBuilderUtil.create();
	static {
		String resource = "mybatis/WeihaiSqlMap.xml";
		InputStream inputStream;
		try {
			inputStream = Resources.getResourceAsStream(resource);
			sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}
	public boolean isContainChinese(String str) {
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(str);
        if (m.find()) {
            return true;
        }
        return false;
    }
	public boolean HasDigit(String content) { //判断字符串中是否含有数字
	    boolean flag = false;
	    Pattern p = Pattern.compile(".*\\d+.*");
	    Matcher m = p.matcher(content);
	    if (m.matches()) {
	        flag = true;
	    }
	    return flag;
	}
	@Scheduled(cron = "0/2 * 6-23  * * ? ")
//	@Scheduled(fixedDelay=500)
	public void jining() {
		// 数据库会话实例
		SqlSession sqlSession = null;
		SmssSendmsgExample example=new SmssSendmsgExample(); 
		String jishu = jedisClient.get("lweihai");
		try {
			// 创建数据库会话实例sqlSession
			sqlSession = sqlSessionFactory.openSession();
			SmssSendmsgMapper mapper = sqlSession.getMapper(SmssSendmsgMapper.class);
			if(StringUtils.isNotBlank(jishu)){
				example.createCriteria().and编号GreaterThan(Integer.parseInt(jishu));
			}else{
				Integer selectMaxId = mapper.selectMaxId();
				if(selectMaxId != null){
					example.createCriteria().and编号GreaterThanOrEqualTo(selectMaxId);
				}
			}
			PageHelper.startPage(1, 10, false);
			PageHelper.orderBy(" 编号 ");
			List<SmssSendmsg> selectByExamples = mapper.selectByExample(example);
//			List<SmssSendmsg> selectByExamples = sqlSession.selectList("com.steam.mapper.SmssSendmsgMapper.selectByExample",example);
			if(selectByExamples != null && selectByExamples.size()>0){
			for (SmssSendmsg tblinfo : selectByExamples) {
				StringBuffer sb = new StringBuffer();
				List<Phone> phone_arr = new ArrayList<>();
				String o_phones = tblinfo.get电话号码();
				String[] phoneList = o_phones.split(" ");
				if(phoneList != null && phoneList.length>0){
                    for (int i=0;i<phoneList.length;i++){
                        if(phoneList[i] != " " && !isContainChinese(phoneList[i]) && HasDigit(phoneList[i])){
                            sb.append(phoneList[i]+" ");
                            Phone phone = new Phone();
                            phone.setPhone(phoneList[i]);
                            phone_arr.add(phone);
                        }
                    }
                }
				
				com.steam.entity.Item items = new com.steam.entity.Item();
				items.setCreateTime(tblinfo.get发送时间());// 创建信息的时间
				items.setUserPhones(sb.toString().trim());// 电话信息
				items.setContent(tblinfo.get信息());//信息的内容
				items.setCity(tblinfo.get出发地());
				items.setFormin("威海");
				//推送给德州网平台bean
				Item item = new Item();
				item.setContent(tblinfo.get信息());
				//item.setPhones(sb.toString().trim());
				item.setTime(tblinfo.get发送时间());
				User user = new User();
				user.setPhoneList(phone_arr);
				item.setUser(user);
				item.setlTime(tblinfo.get发送时间().getTime());
				if(tblinfo.get信息分类().matches("车")){
					items.setTypeId(2);
					item.setTypeId(2);
					try {
						itemMapper.insertSelective(items);
					} catch (Exception e) {
						jedisClient.set("lweihai",tblinfo.get编号().toString());
						e.printStackTrace();
					}
					rabbitTemplate.convertAndSend("*."+items.getFormin(), gson.toJson(item));
				}else{
					items.setTypeId(1);
					item.setTypeId(1);
					try {
						itemMapper.insertSelective(items);
					} catch (Exception e) {
						jedisClient.set("lweihai",tblinfo.get编号().toString());
						e.printStackTrace();
					}
					rabbitTemplate.convertAndSend("*."+items.getFormin(), gson.toJson(item));
				}
				jedisClient.set("lweihai",tblinfo.get编号().toString());
			}
		}
		} catch (Exception e) {
			e.printStackTrace();
			jedisClient.set("lweihai",Integer.parseInt(jishu)+1+"");
		} finally {
			if (sqlSession != null) {
				sqlSession.close();
			}
		}
	}
}